package bTree.LeetCode;

public class 反转字符串中的单词 {
    public String reverseWords(String s) {
        int length = s.length();
        StringBuilder sb=new StringBuilder();
        int left=length-1,right=length-1;
        while (left>=0&&right>=0){
            if(s.charAt(right)!=' '){
                left=right;
                while (left>=0&&s.charAt(left)!=' '){
                    left--;
                }
                sb.append(s.substring(left+1,right+1)+" ");
                right=left;
            }else{
                right--;
            }
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }
}